/**   
* @Title: UserMapper.java
* @Package org.xaoyaoyao.cloud.mybatis.mapper
* @Description: TODO
* @author xaoyaoyao
* @date Jul 3, 2019 3:53:02 PM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import org.xaoyaoyao.cloud.dynamic.annotation.DynamicDataSource;
import org.xaoyaoyao.cloud.mybatis.entity.User;

/**
 * @ClassName: UserMapper
 * @Description: TODO
 * @author xaoyaoyao
 * @date Jul 3, 2019 3:53:02 PM
 * 
 */
@Mapper
@Component
public interface UserMapper {

	/**
	 * 新增用户
	 * 
	 * @param user
	 * @return
	 */
	@DynamicDataSource // 默认数据源
	@Insert("INSERT INTO user(age, password, sex, username) VALUES (#{age}, #{password}, #{sex}, #{username})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 设置id自动增长，设置再对象里
	int save(User user);

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 * @return
	 */
	@DynamicDataSource // 默认数据源
	@Update("UPDATE user SET age=#{age}, password=#{password}, sex=#{sex}, username=#{username} WHERE id =#{id}")
	int update(User user);

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 */
	@DynamicDataSource // 默认数据源
	@Delete("DELETE FROM user WHERE id = #{id}")
	int deleteById(Long id);

	/**
	 * 根据id查询
	 * 
	 * @param id
	 * @return
	 */
	@DynamicDataSource("slave1") // slave1
	@Select("SELECT * FROM user WHERE id = #{id}")
	User selectById(Long id);

	/**
	 * 查询所有用户信息
	 * 
	 * @return
	 */
	@DynamicDataSource("slave2") // slave2
	@Select("SELECT * FROM user")
	List<User> selectAll();

}
